import type { RoomObject } from '@/RoomDesign/domain/entity/share/RoomObject'
import { AxesHelper, Color, GridHelper, Scene, AmbientLight } from 'three'
import type { RoomGroup } from './entity/RoomGroup'

export class ThreeSceneController {
  /**主场景图层 */
  private _scene: Scene

  /**底层图层 */
  private _bottomScene: Scene

  private _entityMap: Map<RoomObject, RoomGroup> = new Map()

  private _ambientLight: AmbientLight | undefined

  public get entityMap(): Map<RoomObject, RoomGroup> {
    return this._entityMap
  }
  public get scene(): Scene {
    return this._scene
  }

  public get bottomScene(): Scene {
    return this._bottomScene
  }

  //#region 场景灯光
  public get ambientLight(): AmbientLight | undefined {
    return this._ambientLight;
  }

  //#endregion 场景灯光

  constructor() {
    this._scene = new Scene()
    this._bottomScene = new Scene()
  }

  public init() {
    this.initScene()
    this.initBottomScene()
  }

  private initScene() {
    // 添加坐标轴辅助器，长度为20
    const axesHelper = new AxesHelper(20)
    this._scene.add(axesHelper)

    // 添加环境光
    const ambientLight = new AmbientLight(0xffffff);
    ambientLight.intensity = 2

    this._ambientLight = ambientLight
    this._scene.add(ambientLight)
  }

  private initBottomScene() {
    // 创建场景
    this._bottomScene.background = new Color(0xeeeeee)

    const gridHelper1 = new GridHelper(10000, 25, 0xcccccc, 0xcccccc)
    const gridHelper2 = new GridHelper(10000, 5, 0x000000, 0x000000)

    gridHelper1.material.depthTest = false
    gridHelper2.material.depthTest = false
    this._bottomScene.add(gridHelper1, gridHelper2)
  }

  public addToScene(houseGroup: RoomGroup): void {
    this._entityMap.set(houseGroup.houseObject, houseGroup)
    this._scene.add(houseGroup)
  }
}
